package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.moudle.message.adapter.ContactAdapter;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.Contact;
import com.bnyy.message.bean.ContactGroup;
import com.bnyy.message.bean.GroupChatSetting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/14
 **/
public class AtGroupChatMemberActivity extends BaseActivityImpl {
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.iv_clear)
    ImageView ivClear;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.cb_select_all)
    CheckBox cbSelectAll;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private ContactAdapter adapter;
    private ArrayList<ContactGroup.Member> members;

    public static void show(Context context, int groupChatId, ActivityResultLauncher<Intent> launcher) {
        Intent intent = new Intent(context, AtGroupChatMemberActivity.class);
        intent.putExtra("groupChatId", groupChatId);
        launcher.launch(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_message_at_group_chat_member;
    }

    @Override
    public String getTitleStr() {
        return "选择提醒的人";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView tvConfirm = new TextView(mContext);
        tvConfirm.setBackgroundResource(R.drawable.selector_button_corner_green_180);
        tvConfirm.setText("确定");
        tvConfirm.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
        tvConfirm.setTextColor(Color.WHITE);
        tvConfirm.setPadding(15, 5, 15, 5);
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<ContactGroup.Member> atMembers = new ArrayList<>();

                members.forEach(new Consumer<ContactGroup.Member>() {
                    @Override
                    public void accept(ContactGroup.Member member) {
                        if (member.isChecked()) {
                            atMembers.add(member);
                        }
                    }
                });
                if (atMembers.size() > 0) {
                    Intent intent = new Intent();
                    intent.putExtra("atMembers", atMembers);
                    setResult(RESULT_OK, intent);
                }
                finish();
            }
        });
        setOptions(tvConfirm);

        adapter = new ContactAdapter(mContext, false,
                new ContactAdapter.CallbackImpl() {
                    @Override
                    public void onCheck(Contact contact, boolean checked, int position) {
                        super.onCheck(contact, checked, position);

                        for (ContactGroup.Member member : members) {
                            if (member.getId() == contact.getId()) {
                                member.setChecked(checked);
                                break;
                            }
                        }
                    }
                });
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);

        int groupChatId = getIntent().getIntExtra("groupChatId", -1);

        HashMap<String, Object> params = new HashMap<>();
        params.put("id", groupChatId);
        requestManager.request(requestManager.mMessageRetrofitService.getGroupChatSetting(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<GroupChatSetting>(mContext) {
                    @Override
                    public void onSuccess(GroupChatSetting groupChatSetting) {
                        super.onSuccess(groupChatSetting);

                        members = groupChatSetting.getUser_list()
                                .stream()
                                .filter(new Predicate<ContactGroup.Member>() {
                                    @Override
                                    public boolean test(ContactGroup.Member member) {
                                        return member.getId() != userInfoManager.getLoginUserId();
                                    }
                                })
                                .collect(Collectors.toCollection(ArrayList::new));
                        adapter.refresh(members);

                        cbSelectAll.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                members.forEach(new Consumer<ContactGroup.Member>() {
                                    @Override
                                    public void accept(ContactGroup.Member member) {
                                        member.setChecked(cbSelectAll.isChecked());
                                    }
                                });

                                adapter.refresh(members);
                            }
                        });
                    }
                });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etSearch.setText("");
                adapter.refresh(members);
            }
        });
        etSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    Editable text = etSearch.getText();
                    if (!TextUtils.isEmpty(text)) {
                        String keyword = text.toString();
                        ArrayList<ContactGroup.Member> target = new ArrayList<>();

                        members.forEach(new Consumer<ContactGroup.Member>() {
                            @Override
                            public void accept(ContactGroup.Member member) {
                                if (member.getUsername().contains(keyword)) {
                                    target.add(member);
                                }
                            }
                        });

                        if (target.size() > 0) {
                            adapter.refresh(target);
                        } else {
                            Toast.makeText(mContext, "无匹配的联系人", Toast.LENGTH_SHORT).show();
                        }
                        return true;
                    }
                }
                return false;
            }
        });
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                ivClear.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
            }
        });

        ivClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etSearch.setText("");
            }
        });
    }
}




































